#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QFile>
#include <QTemporaryFile>
#include <QtDebug>

#include <QDateTime>
#include <QDomDocument>
#include <QFileDialog>

void processing_edge(QDomNodeList edges) {
  for (int i = 0; i < edges.size(); ++i) {
    QDomNode edge = edges.at(i);

    // make edge smooth
    QDomElement e = edge.firstChildElement("y:BendStyle");
    if (!e.isNull()) {
      QDomAttr a = e.attributeNode("smoothed");
      a.setValue("true");
    }

    // change edge color by Y/N 是/否 yes/no
    QString edge_color;
    QString edge_text;

    e = edge.firstChildElement("y:EdgeLabel");
    if (!e.isNull()) {
      edge_text = e.text();
      auto label_text = edge_text.toUpper();

      // if need chage label to upper
      if (edge_text != label_text) {
        edge_text = label_text;
      } else {
        edge_text = "";
      }
      if (edge_text.length() > 0) {
        for (QDomNode n = e.firstChild(); !n.isNull(); n = n.nextSibling()) {
          QDomText t = n.toText();
          if (!t.isNull())
            n.setNodeValue(edge_text);
        }
      }

      // change edge color
      if (label_text == "YES" || label_text == "Y" || label_text == "是") {
        edge_color = "#007f00";
      } else if (label_text == "NO" || label_text == "N" ||
                 label_text == "否") {
        edge_color = "#ff0000";
      }

      // make text slide on edge
      QDomAttr a = e.attributeNode("modelName");
      a.setValue("side_slider");
    }

    if (edge_color.length() > 0) {
      QDomElement LineStyle = edge.firstChildElement("y:LineStyle");
      if (!LineStyle.isNull()) {
        QDomAttr a = LineStyle.attributeNode("color");
        a.setValue(edge_color);
      }
    }

    e = edge.firstChildElement("y:BendStyle");
    if (!e.isNull()) {
      QDomAttr a = e.attributeNode("smoothed");
      a.setValue("true");
    }
  }
}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow) {
  ui->setupUi(this);
}

MainWindow::~MainWindow() { delete ui; }

bool MainWindow::adjust_yed_file(QString filepath) {
  //输入文件
  QFile inputfile(filepath);
  if (!inputfile.open(QIODevice::ReadOnly)) {
    qDebug() << "Open input file failed:" << filepath;
    ui->ptd_info->appendPlainText(
        QDateTime::currentDateTime().toString("hh:mm:ss") +
        " failed:" + filepath);
    return 0;
  }

  QDomDocument doc("yed");
  doc.setContent(&inputfile);

  inputfile.close();

  auto all_edges = doc.elementsByTagName("y:PolyLineEdge");
  processing_edge(all_edges);

  //设置输出文件
  QFile outputfile(filepath);
  if (!outputfile.open(QIODevice::WriteOnly)) {
    qDebug() << "Open output file failed:" << filepath;
    return 0;
  }
  auto new_data = doc.toByteArray();
  outputfile.write(new_data);
  outputfile.close();

  ui->ptd_info->appendPlainText(
      QDateTime::currentDateTime().toString("hh:mm:ss") + " OK:" + filepath);

  return true;
}

void MainWindow::on_ptn_open_clicked() {
  QString fileName = QFileDialog::getOpenFileName(this, tr("Open yed File"), "",
                                                  tr("Yed (*.graphml *.xml)"));

  if (fileName.size() > 0) {
    adjust_yed_file(fileName);
  }
}
